package com.eastjet.equipment.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastjet.equipment.service.DeviceEfdService;
import com.eastjet.equipment.service.DeviceTypeService;
import com.eastjet.maintenance.domain.DeviceEfd;
import com.eastjet.maintenance.domain.DeviceFt;
import com.eastjet.maintenance.domain.DeviceRkb;
import com.eastjet.maintenance.domain.DeviceType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/efd")
public class EfdController {

    @Autowired
    private DeviceEfdService deviceEfdService;


    @RequestMapping("/list")
    public Page<DeviceEfd> getFTList(
            @RequestBody Map<String, Object> map)
    {

        System.out.println("map:" + map);

        // 检查 map 是否为空
        if (map == null || map.isEmpty()) {
            // 返回空的 Page<DeviceFt> 对象
            return  deviceEfdService.getEfdList(0, 0, "");
        }

        Integer page = (Integer) map.get("page");
        Integer size = (Integer) map.get("size");
        String condition = (String) map.get("condition");

        System.out.println("condition:" + condition);
        System.out.println("page:" + page);
        System.out.println("size:" + size);
        return  deviceEfdService.getEfdList(page, size, condition);
    }


    @RequestMapping("/add")
    public String add(@RequestBody Map<String, Object> map) {
        System.out.println("map:" + map);
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }
            DeviceEfd deviceEfd = new DeviceEfd();
            deviceEfd.setCode((String) map.get("code"));
            // 使用 Integer.valueOf 转换整数
            deviceEfd.setEfdName((String) map.get("name"));
            deviceEfd.setEfdEquipmentId(Integer.valueOf(map.get("equipmentId").toString()));
            deviceEfd.setFaultCategory(Integer.valueOf((String) map.get("faultCategory")));
            deviceEfd.setEfdCofId((String) map.get("efdCofId"));
            deviceEfd.setEfdPartId((String) map.get("efdPartId"));
            deviceEfd.setEfdState(Integer.valueOf(map.get("state").toString()));
            deviceEfd.setDescription((String) map.get("description"));
            deviceEfd.setEfdNotes((String) map.get("notes"));
            return deviceEfdService.addDeviceEfd(deviceEfd);

    }

    @RequestMapping("/update")
    public String update(@RequestBody Map<String, Object> map)
    {
        System.out.println("map:" + map);
        if (map == null || map.isEmpty()) {
            return "参" +
                    "数不能为空";
        }

        DeviceEfd deviceEfd = new DeviceEfd();
        deviceEfd.setCode((String) map.get("code"));
        // 使用 Integer.valueOf 转换整数
        deviceEfd.setEfdName((String) map.get("name"));
        deviceEfd.setEfdEquipmentId(Integer.valueOf(map.get("equipmentId").toString()));
        deviceEfd.setFaultCategory(Integer.valueOf((String) map.get("faultCategory")));
        deviceEfd.setEfdCofId((String) map.get("efdCofId"));
        deviceEfd.setEfdPartId((String) map.get("efdPartId"));
        deviceEfd.setEfdState(Integer.valueOf(map.get("state").toString()));
        deviceEfd.setDescription((String) map.get("description"));
        deviceEfd.setEfdNotes((String) map.get("notes"));
        deviceEfd.setEfdId(Integer.valueOf(map.get("id").toString()));
        return deviceEfdService.updateDeviceEfd(deviceEfd);
    }

    @RequestMapping("/delete")
    public String deleteFT( @RequestBody Map<String, Object> map)
    {
        System.out.println("map:" + map);

        // 检查 map 是否为空
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }

        // 获取 state 值
        Integer state = (Integer) map.get("state");
        // 获取 ids 列表
        List<Integer> ids = (List<Integer>) map.get("ids");

        // 创建 DeviceFt 对象并设置其状态
       DeviceEfd deviceEfd = new DeviceEfd();
        deviceEfd.setIsDelete(1);

        // 打印获取到的 state 值
        System.out.println("state: " + state);

        // 循环打印 ids 列表中的每个 ID
        if (ids != null) {
            for (Integer id : ids) {
                System.out.println("id: " + id);
                deviceEfd.setEfdId(id);
                deviceEfdService.deleteDeviceEfd(deviceEfd);
            }
            return "success";
        } else {
            System.out.println("ids 列表为空或未提供");
            return "参数错误";
        }
    }

    @RequestMapping("/updateState")
    public String updateStateFT(@RequestBody Map<String, Object> map) {
        System.out.println("map:" + map);
        // 检查 map 是否为空
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }

        // 获取 state 值
        Integer state = (Integer) map.get("state");
        // 获取 ids 列表
        List<Integer> ids = (List<Integer>) map.get("ids");


        DeviceEfd deviceEfd = new DeviceEfd();
        deviceEfd.setEfdState(state);

        // 打印获取到的 state 值
        System.out.println("state: " + state);

        // 循环打印 ids 列表中的每个 ID
        if (ids != null) {
            for (Integer id : ids) {
                System.out.println("id: " + id);
                deviceEfd.setEfdId(id);
                deviceEfdService.updateDeviceEfd(deviceEfd);
            }
            return "success";
        } else {
            System.out.println("ids 列表为空或未提供");
            return "参数错误";
        }
    }
    @RequestMapping("/listDeviceType")
    public List<DeviceType> listDeviceType()
    {
        return deviceEfdService.getDeviceType();
    }
}
